﻿//============================================================
//author:zhxitao
//============================================================

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Saker.Kernel.Model;

namespace Saker.Kernel.DAL
{
    #region SysModule数据访问层
	/// <summary>
	///SysModule数据访问层
	/// </summary>	
	public partial class SysModuleDAL
	{
        #region SysModule数据访问层 自动生成
		/// <summary>
        /// 添加SysModule
        /// </summary>
        /// <param name="SysModule">SysModule</param>
        /// <returns>添加后的SysModule</returns>
        public SysModule Add
			(SysModule sysModule)
		{
				string sql ="INSERT INTO SysModules (ParentID, ModuleName, ModuleTag, ModuleURL, ModuleDisabled, ModuleOrder, ModuleDescription, IsMenu)  VALUES (@ParentID, @ModuleName, @ModuleTag, @ModuleURL, @ModuleDisabled, @ModuleOrder, @ModuleDescription, @IsMenu) select @@identity";
				SqlParameter[] para = new SqlParameter[]
					{
						new SqlParameter("@ParentID", ToDBValue(sysModule.ParentID)),
						new SqlParameter("@ModuleName", ToDBValue(sysModule.ModuleName)),
						new SqlParameter("@ModuleTag", ToDBValue(sysModule.ModuleTag)),
						new SqlParameter("@ModuleURL", ToDBValue(sysModule.ModuleURL)),
						new SqlParameter("@ModuleDisabled", ToDBValue(sysModule.ModuleDisabled)),
						new SqlParameter("@ModuleOrder", ToDBValue(sysModule.ModuleOrder)),
						new SqlParameter("@ModuleDescription", ToDBValue(sysModule.ModuleDescription)),
						new SqlParameter("@IsMenu", ToDBValue(sysModule.IsMenu)),
					};
					
				int newId = Convert.ToInt32(SqlHelper.ExecuteScalar(sql, para));
				return GetByModuleID(newId);
		}

		/// <summary>
        /// 根据主键删除SysModule
        /// </summary>
        /// <param name="ModuleID">ModuleID</param>
        /// <returns>影响的行数</returns>
        public int DeleteByModuleID(int moduleID)
		{
            string sql = "DELETE SysModules WHERE ModuleID = @ModuleID";

           SqlParameter[] para = new SqlParameter[]
			{
				new SqlParameter("@ModuleID", moduleID)
			};
		
            return SqlHelper.ExecuteNonQuery(sql, para);
		}
		
				
		/// <summary>
		/// 更新SysModule
		/// </summary>
		/// <param name="SysModule">SysModule</param>
		/// <returns></returns>
        public int Update(SysModule sysModule)
        {
            string sql =
                "UPDATE SysModules " +
                "SET " +
			" ParentID = @ParentID" 
                +", ModuleName = @ModuleName" 
                +", ModuleTag = @ModuleTag" 
                +", ModuleURL = @ModuleURL" 
                +", ModuleDisabled = @ModuleDisabled" 
                +", ModuleOrder = @ModuleOrder" 
                +", ModuleDescription = @ModuleDescription" 
                +", IsMenu = @IsMenu" 
               
            +" WHERE ModuleID = @ModuleID";


			SqlParameter[] para = new SqlParameter[]
			{
				new SqlParameter("@ModuleID", sysModule.ModuleID)
					,new SqlParameter("@ParentID", ToDBValue(sysModule.ParentID))
					,new SqlParameter("@ModuleName", ToDBValue(sysModule.ModuleName))
					,new SqlParameter("@ModuleTag", ToDBValue(sysModule.ModuleTag))
					,new SqlParameter("@ModuleURL", ToDBValue(sysModule.ModuleURL))
					,new SqlParameter("@ModuleDisabled", ToDBValue(sysModule.ModuleDisabled))
					,new SqlParameter("@ModuleOrder", ToDBValue(sysModule.ModuleOrder))
					,new SqlParameter("@ModuleDescription", ToDBValue(sysModule.ModuleDescription))
					,new SqlParameter("@IsMenu", ToDBValue(sysModule.IsMenu))
			};

			return SqlHelper.ExecuteNonQuery(sql, para);
        }		
		
		/// <summary>
		/// 根据主键查询SysModule实体
		/// </summary>
		/// <param name="SysModule">SysModule</param>
		/// <returns>SysModule</returns>
        public SysModule GetByModuleID(int moduleID)
        {
            string sql = "SELECT * FROM SysModules WHERE ModuleID = @ModuleID";
            using(SqlDataReader reader = SqlHelper.ExecuteDataReader(sql, new SqlParameter("@ModuleID", moduleID)))
			{
				if (reader.Read())
				{
					return ToModel(reader);
				}
				else
				{
					return null;
				}
       		}
        }
		/// <summary>
		/// SqlDataReader对象转换为SysModule实体
		/// </summary>
		/// <param name="reader">SqlDataReader对象</param>
		/// <returns>SysModule实体</returns>
		public SysModule ToModel(SqlDataReader reader)
		{
			SysModule sysModule = new SysModule();

			sysModule.ModuleID = (int)ToModelValue(reader,"ModuleID");
			sysModule.ParentID = (int)ToModelValue(reader,"ParentID");
			sysModule.ModuleName = (string)ToModelValue(reader,"ModuleName");
			sysModule.ModuleTag = (string)ToModelValue(reader,"ModuleTag");
			sysModule.ModuleURL = (string)ToModelValue(reader,"ModuleURL");
			sysModule.ModuleDisabled = (bool)ToModelValue(reader,"ModuleDisabled");
			sysModule.ModuleOrder = (int)ToModelValue(reader,"ModuleOrder");
			sysModule.ModuleDescription = (string)ToModelValue(reader,"ModuleDescription");
			sysModule.IsMenu = (bool)ToModelValue(reader,"IsMenu");
			return sysModule;
		}
		/// <summary>
		/// 得到数据总行数
		/// </summary>
		/// <returns></returns>
		public int GetTotalCount()
		{
			string sql = "SELECT count(1) FROM SysModules";
			return (int)SqlHelper.ExecuteScalar(sql);
		}
		/// <summary>
		/// 分页，此方法仅在sql 2005下使用
		/// </summary>
		/// <param name="minrownum">起始行数</param>
		/// <param name="maxrownum">>结束行数</param>
		/// <returns>SysModule集合</returns>
		public IEnumerable<SysModule> GetPagedData(int minrownum,int maxrownum)
		{
			string sql = "SELECT * from(SELECT *,row_number() over(order by ModuleID) rownum FROM SysModules) t where rownum>=@minrownum and rownum<=@maxrownum";
			using(SqlDataReader reader = SqlHelper.ExecuteDataReader(sql,
				new SqlParameter("@minrownum",minrownum),
				new SqlParameter("@maxrownum",maxrownum)))
			{
				return ToModels(reader);					
			}
		}
        /// <summary>
        /// 分页，调用存储过程
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="recordCount">记录数</param>
        /// <param name="pageSize">每页记录数</param>
        /// <param name="pageCount">总页数</param>
        /// <param name="whereCondition">where</param>
        /// <returns>DataTable数据集</returns>
        public DataTable GetPagingRecord(int pageIndex, ref int recordCount, int pageSize, ref int pageCount,string whereCondition)
        {
            return SqlHelper.GetPagingRecord(pageIndex,ref recordCount,pageSize,ref pageCount,"SysModules","ModuleID",whereCondition,"*","ModuleID",true);
        }
		/// <summary>
		/// 得到所有SysModule集合
		/// </summary>
		/// <returns>所有集合</returns>
		public IEnumerable<SysModule> GetAll()
		{
			string sql = "SELECT * FROM SysModules";
			using(SqlDataReader reader = SqlHelper.ExecuteDataReader(sql))
			{
				return ToModels(reader);			
			}
		}
		/// <summary>
		/// SqlDataReader对象转换到SysModule集合
		/// </summary>
		/// <param name="reader">SqlDataReader对象</param>
		/// <returns>SysModule集合</returns>
		protected IEnumerable<SysModule> ToModels(SqlDataReader reader)
		{
			var list = new List<SysModule>();
			while(reader.Read())
			{
				list.Add(ToModel(reader));
			}	
			return list;
		}		
		/// <summary>
		/// 转换数据值
		/// </summary>
		/// <param name="value">object类型</param>
		/// <returns>object对象</returns>
		protected object ToDBValue(object value)
		{
			if(value==null)
			{
				return DBNull.Value;
			}
			else
			{
				return value;
			}
		}
		/// <summary>
		/// 转换实体值
		/// </summary>
		/// <param name="reader">SqlDataReader对象</param>
		/// <param name="columnName">列名称</param>
		/// <returns>值</returns>
		protected object ToModelValue(SqlDataReader reader,string columnName)
		{
			if(reader.IsDBNull(reader.GetOrdinal(columnName)))
			{
				return null;
			}
			else
			{
				return reader[columnName];
			}
		}
        #endregion
	}
    #endregion
}
